package com.qf.mapper.impl;

import com.qf.dto.StudentDto;
import com.qf.dto.TeacherDto;
import com.qf.mapper.TeacherMapper;
import com.qf.pojo.Teacher;
import com.qf.utils.DBUtils;

import java.sql.SQLException;
import java.util.List;

public class TeacherMapperImpl implements TeacherMapper {
    @Override
    public void add(String username, String password, String name, String sex, int age, int courseId, String photo) {
        try {
            DBUtils.commonInsert("insert into teacher(username,password,name,sex,age,courseId,photo) values(?,?,?,?,?,?,?)",username,password,name,sex,age, courseId,photo);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void delete(String username) {
        try {
            DBUtils.commonUpdate("delete from teacher where username = ?",username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(String username, String newPassword) {
        try {
            DBUtils.commonUpdate("update teacher set password=? where username=?",newPassword,username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(String username, String name, String sex, int age, int courseId) {
        try {
            DBUtils.commonUpdate("update teacher set name=?,sex=?,age=?,courseId=? where username=?",name,sex,age,courseId,username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(String username, String name, String sex, int age, int courseId, String photo) {
        try {
            DBUtils.commonUpdate("update teacher set name=?,sex=?,age=?,courseId=?,photo=? where username=?",name,sex,age,courseId,photo,username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Teacher getTeacher(String username) {

        Teacher teacher = null;
        try {
            teacher = DBUtils.commonQueryObj(Teacher.class, "select * from teacher where username = ?", username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return teacher;
    }

    @Override
    public Teacher getTeacher(String username, String password) {

        Teacher teacher = null;
        try {
            teacher = DBUtils.commonQueryObj(Teacher.class, "select * from teacher where username = ? and password = ?", username,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return teacher;
    }

    @Override
    public List<Teacher> getTeachers(int offset, int count) {

        List<Teacher> teachers = null;
        try {
            teachers = DBUtils.commonQueryList(Teacher.class, "select * from teacher limit ?,?", offset, count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return teachers;
    }
    @Override
    public List<TeacherDto> getTeachersAndCourses(int offset, int count) {
        List<TeacherDto> list = null;
        try {
            list = DBUtils.commonQueryList(TeacherDto.class, "select t.username,t.name,t.age,t.sex,c.id courseId,c.name as courseName from teacher t join course c on t.courseId=c.id limit ?,?", offset, count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public int getAllCount() {

        int allcount = 0;
        try {
            allcount = DBUtils.getAllCount("teacher");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return allcount;
    }
}
